package com.shqcjd.preinstallmobile.ui.activity;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.shqcjd.preinstallmobile.R;
import com.shqcjd.preinstallmobile.api.ApiService;
import com.shqcjd.preinstallmobile.helper.rxjavahelper.RxObserver;
import com.shqcjd.preinstallmobile.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.preinstallmobile.model.DefectInfoBean;
import com.shqcjd.preinstallmobile.model.ResponseData;
import com.shqcjd.preinstallmobile.ui.adapter.DefectInfoAdapter;
import com.shqcjd.preinstallmobile.ui.base.BaseActivity;
import com.shqcjd.preinstallmobile.ui.base.BasePresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

/**
 * 缺陷详情
 */
public class DefectInfoActivity extends BaseActivity {
    @BindView(R.id.tv_include_title)
    TextView tvIncludeTitle;
    @BindView(R.id.tv_include_finish)
    Button tvIncludeFinish;
    @BindView(R.id.rv_defect_info)
    RecyclerView rvDefectInfo;
    @BindView(R.id.iv_defect_info)
    ImageView ivDefectInfo;
    @BindView(R.id.tv_defect_info_type)
    TextView tvDefectInfoType;
    @BindView(R.id.tv_defect_info_name)
    TextView tvDefectInfoName;
    @BindView(R.id.tv_defect_info_kks)
    TextView tvDefectInfoKks;
    @BindView(R.id.tv_defect_info_state)
    TextView tvDefectInfoState;
    @BindView(R.id.tv_defect_info_inspection_person)
    TextView tvDefectInfoInspectionPerson;
    @BindView(R.id.tv_defect_info_inspection_time)
    TextView tvDefectInfoInspectionTime;
    @BindView(R.id.tv_defect_info_inspect_person)
    TextView tvDefectInfoInspectPerson;
    @BindView(R.id.tv_defect_info_inspect_time)
    TextView tvDefectInfoInspectTime;
    @BindView(R.id.tv_defect_info)
    TextView tvDefectInfo;
    private Context context;

    private List<String> mList = new ArrayList<>();
    private DefectInfoAdapter defectInfoAdapter;
    private int dId;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_defect_info;
    }

    @Override
    public void init() {
        super.init();
        dId = getIntent().getIntExtra("dId", 1);
    }

    @Override
    public void initView() {
        super.initView();
        context = this;
        tvIncludeTitle.setText("缺陷详情");

        tvIncludeFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        getData();
    }

    private void getData() {
        ApiService.defectInfoHttp(dId)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<DefectInfoBean>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                        showWaitingDialog("正在加载...");
                    }

                    @Override
                    public void _onNext(ResponseData<DefectInfoBean> responseData) {
                        if (responseData.retCode != 10000) {
                            return;
                        }
                        if (defectInfoAdapter == null) {
                            rvDefectInfo.setLayoutManager(new GridLayoutManager(context, 3));
                            defectInfoAdapter = new DefectInfoAdapter(responseData.data.img, context);
                            rvDefectInfo.setAdapter(defectInfoAdapter);
                        }
                        RequestOptions options = new RequestOptions()
                                .placeholder(R.mipmap.ic_default_small)    //加载成功之前占位图
                                .error(R.mipmap.ic_default_small)    //加载错误之后的错误图
                                .centerCrop();//指定图片的缩放类型为centerCrop （等比例缩放图片，直到图片的宽高都
                        Glide.with(context)
                                .load(responseData.data.equipment_type_img)
                                .apply(options)
                                .into(ivDefectInfo);
                        tvDefectInfoType.setText("设备类型:"+responseData.data.equipment_type_name);
                        tvDefectInfoName.setText("设备名称:"+responseData.data.equipment_name);
                        tvDefectInfoKks.setText("KKS:"+responseData.data.kks);
                        tvDefectInfoState.setText("状态:"+responseData.data.handle_state);
                        tvDefectInfoInspectionPerson.setText("巡检员:"+responseData.data.check_user);
                        tvDefectInfoInspectionTime.setText("巡检时间:"+responseData.data.check_time);
                        tvDefectInfoInspectPerson.setText("检修员:"+responseData.data.handle_user);
                        tvDefectInfoInspectTime.setText("检修时间:"+responseData.data.handle_time);
                        tvDefectInfo.setText("      "+responseData.data.info);

                    }

                    @Override
                    public void _onError(String errorMessage) {
                        hideWaitingDialog();
                        Log.e("tou", errorMessage);
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        hideWaitingDialog();
                    }
                });
    }

}
